package org.smart4j.chapter1.plugin.security;

import org.apache.shiro.web.env.EnvironmentLoaderListener;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.util.Set;

/**
 * @ClassName: Smart Security 插件
 * @Description: 在Web应用初始化的时候，完成注册Shiro相关细节
 * 包括配置文件
 * 要实现该支持，只需实现javax.servlet.ServletContainerInitializer接口
 * 并在classpath下的META-INF/services/javax.servlet.ServletContainerInitializer文件中添加需要初始化的类即可
 * 然后在上述文件中，添加一行代码：
 * org.smart4j.plugin.security.SmartSecurityPlugin
 * @Author: LynnZou
 * @Date: 2017/5/31
 */
public class SmartSecurityPlugin implements ServletContainerInitializer{

    @Override
    public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
        //设置初始化参数
        servletContext.setInitParameter("shiroConfigLocations","classpath:smart-security.ini");
        //记录Listener
        servletContext.addListener(EnvironmentLoaderListener.class);
        //记录Filter
        FilterRegistration.Dynamic smartSecurityFilter = servletContext.addFilter("SmartSecurityFilter", SmartSecurityFilter.class);
        smartSecurityFilter.addMappingForUrlPatterns(null,false,"/*");

    }
}
